package cn.appsys.controller.backend;

import cn.appsys.pojo.BackendUser;
import cn.appsys.service.backend.BackendUserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class BackendUserController {
    @Autowired
    private BackendUserService userService;
    private static Logger logger = Logger.getLogger(BackendUserController.class);

    @RequestMapping(value = "/backend/adminLogin")
    public String login() {
        return "backend/login";
    }

    @RequestMapping(value = "/admin/login")
    @ResponseBody
    public Map<String, Object> dologin(@RequestParam String username, @RequestParam String password, HttpSession session, HttpServletRequest request) {
        logger.debug("管理员登陆DoLogin!!!!");
        //调用service方法进行用户登录
        BackendUser backendUser = userService.login(username, password);
        Map map = new HashMap();
        if (backendUser == null) {
            map.put("success", false);
            map.put("message", "用户名或者密码错误");
            return map;
        }
        request.setAttribute("error", "用户名或者密码错误");
        session.setAttribute("admin", backendUser);
        map.put("success", true);
        map.put("message", "登陆成功");
        return map;
    }

    @RequestMapping(value = "/admin/loginsuccess")
    public String loginsuccess() {
        return "backend/main";
    }

}
